package com.channel.temperature.collect.ui.activity

import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Observer
import com.hjq.toast.ToastUtils
import com.channel.temperature.collect.R
import com.channel.temperature.collect.app.base.BaseActivity
import com.channel.temperature.collect.app.dao.TaskDataBase
import com.channel.temperature.collect.app.ext.LiveDataEvent
import com.channel.temperature.collect.app.ext.initBack
import com.channel.temperature.collect.data.response.TaskBean
import com.channel.temperature.collect.databinding.ActivityEditTaskDataBinding
import com.channel.temperature.collect.ui.adapter.TaskEditAdapter
import com.channel.temperature.collect.ui.viewmodel.EditTaskDataModel
import me.hgj.mvvmhelper.ext.divider
import me.hgj.mvvmhelper.ext.getColorExt
import me.hgj.mvvmhelper.ext.grid
import me.hgj.mvvmhelper.ext.setOnclick
import me.hgj.mvvmhelper.util.decoration.DividerOrientation

class EditTaskDataActivity : BaseActivity<EditTaskDataModel, ActivityEditTaskDataBinding>() {
    private val mTaskAdapter: TaskEditAdapter by lazy { TaskEditAdapter() }
    override fun initView(savedInstanceState: Bundle?) {
        mToolbar.initBack("批量编辑") {
            finish()
        }
        mBind.listRecyclerView.grid(1).divider {
            orientation = DividerOrientation.GRID
            includeVisible = true
            setDivider(1, true)
            setColor(getColorExt(R.color.common_text_hint_color))
        }.adapter = mTaskAdapter
        mViewModel.getListData(this)
        mTaskAdapter.setOnItemClickListener { adapter, view, position ->
            var itemBean = adapter.data.get(position) as TaskBean
            var isSelected = itemBean.isSelected
            if (isSelected) {
                itemBean.isSelected = false
            } else {
                itemBean.isSelected = true
            }
            mTaskAdapter.setData(position, itemBean)
            mTaskAdapter.notifyDataSetChanged()
        }
    }

    override fun onRequestSuccess() {
        mViewModel.tasklistData.observe(this, Observer {
            mTaskAdapter.setNewInstance(it)
            mTaskAdapter.notifyDataSetChanged()
        })

    }

    override fun onBindViewClick() {
        //绑定点击事件
        setOnclick(mBind.selectAll, mBind.save) {
            when (it) {
                mBind.selectAll -> {
                    if (mTaskAdapter.data.size > 0) {
                        for (i in 0 until mTaskAdapter.data.size) {
                            var item = mTaskAdapter.data.get(i)
                            item.isSelected = true
                            mTaskAdapter.data.set(i, item)
                        }
                        mTaskAdapter.notifyDataSetChanged()
                    }

                }

                mBind.save -> {
                    var savedListData = ArrayList<TaskBean>()
                    if (mTaskAdapter.data.size > 0) {
                        for (i in 0 until mTaskAdapter.data.size) {
                            if (mTaskAdapter.data.get(i).isSelected) {
                                savedListData.add(mTaskAdapter.data.get(i))
                            }
                        }

                    }
                    if (savedListData.size > 0) {
                        TaskDataBase.get(this).taskDao().deleteAll()
                        TaskDataBase.get(this).taskDao().insertAll(*savedListData.toTypedArray())
                        LiveDataEvent.updateTaskRecycler.value = true //通知刷新列表数据
                        EditTaskDataActivity@ this.finish()
                        Log.e(
                            "得到的值",
                            "" + savedListData.size + "得到的值  " + TaskDataBase.get(this)
                                .taskDao()
                                .getAll()?.size
                        );
                    } else {
                        ToastUtils.show("请先选择任务再保存")
                    }

                }
            }
        }
    }

}